package cn.jhz.learn.blog.config.datasource;

import com.alibaba.druid.pool.DruidDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.PropertiesLoaderUtils;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.lang.NonNull;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.TransactionManagementConfigurer;

import javax.sql.DataSource;
import java.io.IOException;
import java.util.Properties;

@Configuration
@MapperScan(basePackages={"cn.jhz.learn.blog.dao.account"}, sqlSessionFactoryRef = "accountSqlSessionFactory")
public class AccountDataConfig {

    @Bean("accountDataSource")
    public DataSource dataSource() {
        try {
            Resource resource = new ClassPathResource("db.properties");
            Properties props = PropertiesLoaderUtils.loadProperties(resource);
            DruidDataSource ds = new DruidDataSource();

            ds.setDriverClassName(props.getProperty("jdbc.driver"));
            ds.setUrl(props.getProperty("jdbc.account.url"));
            ds.setUsername(props.getProperty("jdbc.username"));
            ds.setPassword(props.getProperty("jdbc.password"));
            ds.setInitialSize(3);
            ds.setMinIdle(3);
            ds.setMaxActive(20);
            ds.setMaxWait(60000);

            return ds;
        }catch (IOException ioE) {
            ioE.printStackTrace();
        }
        return null;
    }

    @Bean("accountSqlSessionFactory")
    public SqlSessionFactory sqlSessionFactory(
            @Qualifier("accountDataSource")DataSource dataSource) throws Exception {

        SqlSessionFactoryBean sf = new SqlSessionFactoryBean();

        sf.setDataSource(dataSource);
        sf.setMapperLocations(
                new PathMatchingResourcePatternResolver().getResources("classpath:mapper/account/*.xml"));
        return sf.getObject();
    }

    @Bean("accountSqlSessionTemplate")
    public SqlSessionTemplate sqlSessionTemplate(@Qualifier("accountSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory); // 使用上面配置的Factory
    }


    @Bean(name = "accountTransactionManager")
    @NonNull
    public PlatformTransactionManager annotationDrivenTransactionManager() {
        DataSourceTransactionManager transactionManager = new DataSourceTransactionManager();
        //设置事务管理器管理的数据源
        transactionManager.setDataSource(dataSource());
        return transactionManager;
    }
}